package com.itheima.controller;

import com.itheima.anno.LogOperate;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    //查询部门列表
    @RequestMapping("/depts")
    public Result list() {
        log.info("查询部门列表");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }

    /**
     * 根据id删除部门
     * @param id
     */
    @DeleteMapping("/depts")
    @LogOperate
    public Result delete(Integer id){
        log.info("根据id删除部门,id: {}" + id);
        deptService.deleteById(id);
        return Result.success();
    }
    /**
     * 新增
     * @param
     */
    @PostMapping("/depts")
    @LogOperate
    public Result save(@RequestBody Dept dept){
        log.info("新增部门, dept: {}" + dept);
        deptService.save(dept);
        return Result.success();
    }
    /**
     * 根据ID查询 - GET http://localhost:8080/depts/1
     */
    @GetMapping("/depts/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据ID查询, id:{}" + id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }
    /**
     * 修改部门 - PUT http://localhost:8080/depts  请求参数：{"id":1,"name":"研发部"}
     */
    @PutMapping("/depts")
    @LogOperate
    public Result update(@RequestBody Dept dept){
        log.info("修改部门, dept: {}",dept);
        deptService.update(dept);
        return Result.success();
    }
}
